<?php
/**
 * Created by PhpStorm.
 * User: 俊伟
 * Date: 2021/5/21
 * Time: 19:58
 */

namespace app\api\model;


use app\lib\exception\banner\BannerException;
use think\Db;
use think\Exception;
use think\model\concern\SoftDelete;

class Banner extends BaseModel
{
    use SoftDelete;
    /**
     * @param $params
     * @throws BannerException
     */
    public static function add($params)
    {
        // 启动事务
        Db::startTrans();
        try {
            // 调用当前模型的静态方法create()，第一个参数为要写入的数据，第二个参数标识仅写入数据表定义的字段数据
            $banner = self::create($params, true);
            // 调用关联模型实现关联写入
            $banner->items()->saveAll($params['items']);
            // 提交事务
            Db::commit();
        } catch (Exception $ex) {
            // 回滚事务
            Db::rollback();
            throw new BannerException([
                'msg' => '新增轮播图失败',
                'error_code' => 70001
            ]);
        }

    }
    public function items(){
        return $this->hasMany(BannerItem::class,"banner_id","id");
    }




}